// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: implementation of CMSHTMLCSS3Handler.
//   $Id$


#include "StdAfx.h"
#include "mshtmlcss3handler.h"
#include <common/FileSystem/msw/FileSystemUtils.h>

typedef std::pair<CComBSTR,bool> CPairCComBSTRAndbool;

CMSHTMLCSS3Handler::CMSHTMLCSS3Handler(IHTMLDocument2 * pTargetDocument,const char * const sPathToCSS,const char * const sChromePath)
	:m_pTargetDocument(pTargetDocument)
{
	if(pTargetDocument && sPathToCSS)
	{
		char * pcCssFileContent;
		HRESULT hr = xport::ReadFromFileSystem(sPathToCSS,reinterpret_cast<void**>(&pcCssFileContent));
		if(SUCCEEDED(hr))
		{
			m_sCssFileContent = pcCssFileContent;
			//ReadFromFileSystem calls malloc - What is this - the dark ages?
			free(pcCssFileContent);

			m_sCssFileContent.Replace("chrome://",sChromePath);
		}
		else
		{
			throw hr;
		}
	}
	else
	{
		throw(E_INVALIDARG);
	}
}

const CComBSTR g_bsNothing(L"");
HRESULT CMSHTMLCSS3Handler::AddNamespacePrefix(BSTR bsPrefix)
{
	HRESULT hr = E_PENDING;
	if(!m_sCssFileContent.IsEmpty())
	{
		//first check to see if bsPrefix has already been imported.
		CMapCComBSTRTobool::const_iterator iter = m_lstNamespacePrefixesAlreadyImported.find(bsPrefix);
		if(iter == m_lstNamespacePrefixesAlreadyImported.end())
		{
			CComPtr<IHTMLStyleSheet> pCSS;
			//create a blank stylesheet to have rules added to it.
			//	NB: don't try to add the rules in the first parameter here,
			// 	it doesn't work properly.  See Below.
			hr = m_pTargetDocument->createStyleSheet(g_bsNothing,0,&pCSS);
			if(SUCCEEDED(hr) && pCSS)
			{
				//Having failed to find bsPrefix, import it.
				//create the jscript css argument.
				CStringW sCSSArg(m_sCssFileContent);
				//This mechanism doesn't work.  Whatever content is added, 
				//	the message: "(1 item remaining)" appears in the status bar
				//	and doesn't go away until some User action occurs on the form.
				//	Harmless, this may sound, but it also blocks FindBehaviour for repeat 
				//	iterations in version 2.

			//	sCSSArg.Format(L"javascript:'%S'",m_sCssFileContent);

				//At this point, all xforms namespace element rules are in the 
				//	css3 format i.e. xforms|banana{}.  They should be replaced with
				//	the css <=2.1 fudge: xforms\:banana{}
				CStringW sPrefix(bsPrefix);
				sPrefix += L"\\:";
				sCSSArg.Replace(L"xforms|",sPrefix);

				CComBSTR sDefaultCSS(sCSSArg);

				pCSS->put_cssText(sDefaultCSS);
				//add the prefix to the list of already imported prefixes, so that next time, 
				//	we can return S_FALSE.
				m_lstNamespacePrefixesAlreadyImported.insert(CPairCComBSTRAndbool(bsPrefix,true));
			}
		}
		else
		{
			hr = S_FALSE;
		}
	}

	return hr;
}

CMSHTMLCSS3Handler::~CMSHTMLCSS3Handler(void)
{
}
